/*
* 文 件 名:  UserDAOImpl.java
* 版    权:  Tydic Technologies Co., Ltd. Copyright 1993-2012,  All rights reserved
* 描    述:  用户信息数据库操作实现
* 修 改 人:  agan
* 修改时间:  2012-9-4
* 跟踪单号:  <跟踪单号>
* 修改单号:  <修改单号>
* 修改内容:  <修改内容>
*/

package com.tydic.simple.dao.user.impl;

import java.sql.SQLException;
import java.util.List;
import java.util.ArrayList;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;

import com.tydic.framework.dao.AbstractIbatisDAO;
import com.tydic.framework.dataobject.ResultDTO;
import com.tydic.framework.exception.DAOException;
import com.tydic.simple.dao.user.IUserDAO;
import com.tydic.simple.dto.user.UserDTO;
import com.tydic.simple.model.user.User;

/**
 * 
 * 用户信息操作
 * 
 * @author  agan
 * @version  [1.0, 2012-9-4]
 * @since  [权限管理/1.0]
 */
public class UserDAOImpl extends AbstractIbatisDAO implements IUserDAO {
    
    private static final Logger LOGGER = LoggerFactory.getLogger(UserDAOImpl.class);
    
    /** {@inheritDoc} */
    @Override
    public Long saveUser(User user)
        throws DAOException {
        try {
            sqlMapClient.insert("user.INSERT_USER", user);
        }
        catch (SQLException e) {
            LOGGER.error("saveUser exception",e);
            throw new DAOException("saveUser exception", e);
        }
        return user.getUserId();
    }
    
    /** {@inheritDoc} */
    @Override
    @CacheEvict(value = "defaultCache", key = "#loginId")
    public int deleteUserByLoginId(String loginId)
        throws DAOException {
        try {
            return sqlMapClient.delete("user.DELETE_USER_BY_LOGINID", loginId);
        }
        catch (SQLException e) {
            LOGGER.error("deleteUserByLoginId exception",e);
            throw new DAOException("deleteUserByLoginId exception", e);
        }
    }
    
    /** {@inheritDoc} */
    @Override
    @Cacheable(value = "defaultCache", key = "#loginId")
    public User findUserByLoginId(String loginId)
        throws DAOException {
        try {
            return (User)sqlMapClient.queryForObject("user.FIND_USER_BY_LOGINID", loginId);
        }
        catch (SQLException e) {
            LOGGER.error("findUserByLoginId exception",e);
            throw new DAOException("findUserByLoginId exception", e);
        }
    }
    
    /** {@inheritDoc} */
    @SuppressWarnings("unchecked")
    @Override
    @Cacheable(value = "defaultCache", key = "#userDTO.hashCode()")
    public ResultDTO<User> findUserForPage(UserDTO userDTO)
        throws DAOException {
        
        ResultDTO<User> result = new ResultDTO<User>();
        try {
            List<User> userList = sqlMapClient.queryForList("user.FIND_USER_PAGE", userDTO);
            result.setResult(userList != null ? userList : new ArrayList<User>());
            BeanUtils.copyProperties(userDTO.getPagination(), result.getPagination());
            result.getPagination().setTotal((Integer)sqlMapClient.queryForObject("user.FIND_USER_PAGE_COUNT", userDTO));
        }
        catch (SQLException e) {
            LOGGER.error("findUserForPage exception",e);
            throw new DAOException("findUserForPage exception", e);
        }
        return result;
    }
    
    /** {@inheritDoc} */
    @SuppressWarnings("unchecked")
    @Override
    public List<User> findUserForList(UserDTO userDTO)
        throws DAOException {
        try {
            List<User> userList = sqlMapClient.queryForList("user.FIND_USER", userDTO);
            return userList != null ? userList : new ArrayList<User>();
        }
        catch (SQLException e) {
            LOGGER.error("findUserForList exception",e);
            throw new DAOException("findUserForList exception", e);
        }
    }
    
    /** {@inheritDoc} */
    @Override
    public User findUserForSingle(UserDTO userDTO)
        throws DAOException {
        List<User> userList = findUserForList(userDTO);
        return userList.size() > 0 ? userList.get(0) : null;
    }

     /** {@inheritDoc} */
    @Override
    public int updateUser(User user)
        throws DAOException {
        try {
            return sqlMapClient.update("user.UPDATE_USER_BY_LOGINID", user);
        }
        catch (SQLException e) {
            LOGGER.error("updateUser exception",e);
            throw new DAOException("updateUser exception", e);
        }
    }
    
}
